package com.ym.soso.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ym.common.constant.DictEnum;
import com.ym.common.utils.HtmlHandleUtil;
import com.ym.common.utils.StringUtil;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.enums.CodeRuleEnum;
import com.ym.medical.enums.CollectLableTypeEnum;
import com.ym.medical.service.ISysCodeRuleSV;
import com.ym.medical.service.IUserCollectionSV;
import com.ym.soso.dao.PensionInfoDao;
import com.ym.soso.domain.entity.PensionInfoEntity;
import com.ym.soso.domain.param.PensionInfoAppQryParam;
import com.ym.soso.domain.param.PensionInfoQueryParam;
import com.ym.soso.domain.param.PensionInfoUpdateParam;
import com.ym.soso.domain.param.PublishParam;
import com.ym.soso.domain.vo.PensionInfoListPcVO;
import com.ym.soso.domain.vo.PensionInfoSimVO;
import com.ym.soso.enums.AdmittedEnum;
import com.ym.soso.enums.ChargeLevelEnum;
import com.ym.soso.enums.PublishStatusEnum;
import com.ym.soso.enums.SpecServiceEnum;
import com.ym.soso.service.IPensionInfoSV;
import com.ym.soso.service.search.service.PensionInfoIndexService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 养老Service业务层处理
 *
 * @author ym
 * @date 2020-09-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PensionInfoSVImpl extends BaseSVImpl<PensionInfoEntity, PensionInfoDao> implements IPensionInfoSV {

	@Autowired
	private ISysCodeRuleSV sysCodeRuleSV;

	@Autowired
	private PensionInfoIndexService pensionInfoIndexService;

	/**
	 * 查询养老列表
	 *
	 * @param queryParam 养老 查询参数
	 * @return 养老
	 */
	@Override
	public List<PensionInfoListPcVO> selectListPC(PensionInfoQueryParam queryParam) {
		if (StringUtils.isNotBlank(queryParam.getAdmittedType())) {
			AdmittedEnum dictEnum = DictEnum.valueOfEnum(AdmittedEnum.class, queryParam.getAdmittedType());
			queryParam.setAdmittedType(dictEnum.getValue());
		}

		if (StringUtils.isNotBlank(queryParam.getChargeLevel())) {
			ChargeLevelEnum dictEnum = DictEnum.valueOfEnum(ChargeLevelEnum.class, queryParam.getChargeLevel());
			String value = dictEnum.getValue();
			String floor = null;
			String upper = null;
			if (StringUtils.isNotBlank(value) && value.contains("-")) {
				String[] split = value.split("-");
				switch (dictEnum) {
					case FIRST:
						upper = split[0];
						break;
					case SIXTH:
						floor = split[0];
						break;
					default:
						floor = split[1];
						upper = split[1];
						break;
				}
			}
			if (StringUtils.isNotBlank(floor)) {
				queryParam.setChargeFloor(Double.valueOf(floor));
			}
			if (StringUtils.isNotBlank(upper)) {
				queryParam.setChargeUpper(Double.valueOf(upper));
			}
		}
		return mapper.selectListPC(queryParam);
	}

	/**
	 * 新增养老
	 *
	 * @param updateParam 养老 新增参数
	 * @return 结果
	 */
	@Override
	public Long insertPC(PensionInfoUpdateParam updateParam) {
		PensionInfoEntity entity = updateParam.covert();
		entity.setCreSb(ShiroUtil.getLoginId());
		String code = sysCodeRuleSV.createCode(CodeRuleEnum.COMPREHENSIVE_CLINIC);
		entity.setTheCode(code);
		entity.setPublishStatus(PublishStatusEnum.UN_PUBLISHED.getKey());
		int result = mapper.insert(entity);
		if (result > 0) {
			searchUpdateIndex(entity.getId());
		}

		return entity.getId();
	}

	/**
	 * 修改养老
	 *
	 * @param updateParam 养老 修改参数
	 * @return 结果
	 */
	@Override
	public int updateByIdPC(PensionInfoUpdateParam updateParam) {
		PensionInfoEntity entity = updateParam.covert();
		entity.setUptSb(ShiroUtil.getLoginId());
		int result = this.updateById(entity);

		if (result > 0) {
			searchUpdateIndex(entity.getId());
		}

		return result;
	}

	@Override
	public List<PensionInfoSimVO> listPensionInfo(PensionInfoAppQryParam param) {
		int[] specServices = param.getSpecServices();
		if (specServices != null && specServices.length == 6) {
			String join = StrUtil.join("", specServices);
			String keys = SpecServiceEnum.getKeys(join);
			if (!"00".equals(keys)) {
				param.setSpecServicesStr(keys);
			}
		}

		List<Long> ids = pensionInfoIndexService.searchByKeywordOfIds(param);
		if (StringUtil.isNotBlank(param.getKeyword()) && ids.isEmpty()) {
			return Collections.emptyList();
		}
		if (!ids.isEmpty()) {
			param.setIds(ids.toArray(new Long[ids.size()]));
		}
		if (StringUtil.isNotBlank(param.getKeyword())) {
			param.setKeywordSort(true);
		}

		//2.价格筛选
		if (StringUtils.isNotBlank(param.getChargeLevel())) {
			ChargeLevelEnum dictEnum = DictEnum.valueOfEnum(ChargeLevelEnum.class, param.getChargeLevel());
			String value = dictEnum.getValue();
			String floor = null;
			String upper = null;
			if (StringUtils.isNotBlank(value) && value.contains("-")) {
				String[] split = value.split("-");
				switch (dictEnum) {
					case FIRST:
						upper = split[0];
						break;
					case SIXTH:
						floor = split[0];
						break;
					default:
						floor = split[0];
						upper = split[1];
						break;
				}
			}
			if (StringUtils.isNotBlank(floor)) {
				param.setChargeFloor(Double.valueOf(floor));
			}
			if (StringUtils.isNotBlank(upper)) {
				param.setChargeUpper(Double.valueOf(upper));
			}
		}
		List<PensionInfoSimVO> pensionInfoSimVOS = mapper.listPensionInfo(param);
		return pensionInfoSimVOS;
	}

	@Override
	public void release(PublishParam publishParams) {
		int result = this.updateByIds(PensionInfoEntity.builder()
				.publishStatus(publishParams.getState())
				.publisher(ShiroUtil.getLoginId())
				.publishTime(new Date())
				.build(), publishParams.getIds());

		if (result > 0) {
			List<PensionInfoEntity> datas = this.selectList(SelectWhere().andIn(PensionInfoEntity::getId, publishParams.getIds()).end());
			pensionInfoIndexService.saveOrUpdate(datas);
		}
	}

	@Autowired
	private IUserCollectionSV userCollectionSV;

	@Override
	public PensionInfoSimVO infoMobile(Long id, int width) {
		PensionInfoAppQryParam qryParam = new PensionInfoAppQryParam();
		Long[] longs = new Long[]{id};
		qryParam.setIds(longs);
		List<PensionInfoSimVO> pensionInfos = mapper.listPensionInfo(qryParam);

		PensionInfoSimVO pensionInfoSimVO = new PensionInfoSimVO();
		if (!pensionInfos.isEmpty()) {
			pensionInfoSimVO = pensionInfos.get(0);
			pensionInfoSimVO.setIntroduction(HtmlHandleUtil.resize(pensionInfoSimVO.getIntroduction(), width, false));
			pensionInfoSimVO.setServiceItem(HtmlHandleUtil.resize(pensionInfoSimVO.getServiceItem(), width, false));
			pensionInfoSimVO.setRates(HtmlHandleUtil.resize(pensionInfoSimVO.getRates(), width, false));
			pensionInfoSimVO.setFacility(HtmlHandleUtil.resize(pensionInfoSimVO.getFacility(), width, false));
			pensionInfoSimVO.setRoomNotes(HtmlHandleUtil.resize(pensionInfoSimVO.getRoomNotes(), width, false));
			long userCollection = userCollectionSV.selectAttention(id, ShiroUtil.getLoginId(true), CollectLableTypeEnum.PENSION_INFO);
			pensionInfoSimVO.setColectFlag(userCollection != 0);
		}
		return pensionInfoSimVO;
	}

	/**
	 * 查询数据并更新
	 */
	private void searchUpdateIndex(Long id) {
		PensionInfoEntity entity = this.selectById(id);
		if (entity != null) {
			pensionInfoIndexService.saveOrUpdate(entity);
		}
	}
}
